package com.skp.shaphan.savedConnections;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;

import javax.swing.JButton;

import com.skp.shaphan.ui.connectioncontrols.ConnectionOptions;
import com.skp.shaphan.ui.connectioncontrols.SavedConnectionOptions;
import com.skp.shaphan.ui.dialogs.ConnectionDialog;

public class NewSavedConnection extends JButton implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3318044710367174072L;
	ConnectionDialog dialog;
	
	public NewSavedConnection(ConnectionDialog dialog) {
		super("Save As New");
		this.addActionListener(this);
		this.dialog = dialog;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		ConnectionOptions options = dialog.getCurrentConnectionOptions();
		Properties properties = options.getConnectionOptions();
		String connectionName = dialog.getConnectionName();
		SavedConnections savedConnections = SavedConnections.getInstance();
		savedConnections.newConnection(connectionName, options, properties);

		SavedConnectionOptions savedOptions = (SavedConnectionOptions)dialog.getTabForClass(SavedConnectionOptions.class.getName());
		savedOptions.refreshConnectionTable();
		dialog.changeToTab(SavedConnectionOptions.class);
	}

}
